package base;

import db.FoodManager;
import db.IPersistable;
import db.UserManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Shayan
 */
public class Intake extends Entity implements IPersistable {

    public Intake() {
        date = new Date();
    }

    public Intake(String userId, String foodId, float quantity, String unit, Date date) {
        this.userId = userId;
        this.foodId = foodId;
        this.quantity = quantity;
        this.unit = unit;
        this.date = date;
    }
    /**
     * Quantity
     */
    private float quantity;

    public float getQuantity() {
        return quantity;
    }

    public void setQuantity(float quantity) {
        this.quantity = quantity;
    }
    /**
     * Unit
     */
    private String unit;

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }
    /**
     * Date
     */
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    /**
     * Food
     */
    private String foodId;

    public String getFoodId() {
        return foodId;
    }

    public void setFoodId(String foodId) {
        if (!foodId.equalsIgnoreCase(this.foodId)) {
            this.foodId = foodId;
            cachedFood = null;
        }
    }
    private Food cachedFood = null;

    public Food getFood()
            throws RecordStoreException, RecordStoreNotOpenException, InvalidRecordIDException, IOException {
        if (cachedFood == null) {
            cachedFood = FoodManager.getById(foodId);
        }
        return cachedFood;
    }

    /**
     * Calories
     */
    public float getCalories()
            throws RecordStoreException, RecordStoreNotOpenException, InvalidRecordIDException, IOException {
        return quantity * getFood().getCalories() / 1000;
    }
    /**
     * User
     */
    private String userId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public User getUser() throws RecordStoreException, IOException {
        return UserManager.getById(userId);
    }

    public void validate() throws ValidationException {
    }

    public byte[] getData() throws IOException {
        ByteArrayOutputStream bin = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bin);
        out.writeUTF(getId());
        out.writeUTF(userId);
        out.writeLong(date.getTime());
        out.writeUTF(foodId);
        out.writeUTF(unit);
        out.writeFloat(quantity);

        out.flush();
        return bin.toByteArray();
    }

    public static Intake create(byte[] data) throws IOException {
        Intake intake = new Intake();
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream in = new DataInputStream(bin);

        intake.setId(in.readUTF());
        intake.setUserId(in.readUTF());
        intake.setDate(new Date(in.readLong()));
        intake.setFoodId(in.readUTF());
        intake.setUnit(in.readUTF());
        intake.setQuantity(in.readFloat());

        return intake;
    }
}
